package com.mall507.fivepeaks.activity.main;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.widget.xrefresh.XSwipeRefreshLayout;
import com.mall507.comn.model.ConvenientLifeZhaoPin;
import com.mall507.comn.model.ConvenientLifeZhaoPinInfo;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.community.CommunityBusinessStoreInfoRUI;
import com.mall507.fivepeaks.comn.gsonBean.ShopMainJsonBean;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.xdialog.XAddressDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/main/MainZhaoPinInfoUI")
public class MainZhaoPinInfoUI extends BaseUI {

    @Autowired(name = "id")
    String id;
    @ViewInject(R.id.head_banner)
    BGABanner head_banner;
    @ViewInject(R.id.titles)
    TextView titles;
    @ViewInject(R.id.salary)
    TextView salary;
    @ViewInject(R.id.small_title)
    TextView small_title;
    @ViewInject(R.id.time)
    TextView time;
    @ViewInject(R.id.company)
    TextView company;
    @ViewInject(R.id.describe)
    TextView describe;
    @ViewInject(R.id.address)
    TextView address;
    @ViewInject(R.id.contacts)
    TextView contacts;
    @ViewInject(R.id.phone)
    TextView phone;
    @ViewInject(R.id.call)
    Button call;
    @ViewInject(R.id.dao)
    Button dao;
    String Address = "";
    String callNumber = "";
    List<String> listUrl;
    String Addresslat = "";
    String Addresslng = "";

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_main_zhaopin_info);
        getTitleView().setContent("招聘详情");
        showLoding();
        loadNetWorkData();
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = phone.getText().toString();
                callNumber = str.substring(5,str.length());
                call(callNumber);
            }
        });
        dao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new XAddressDialog().show(MainZhaoPinInfoUI.this, Address, Addresslng,Addresslat, new XAddressDialog.XUserMainSignRewardDialogCallBack() {
                    @Override
                    public void confirm() {

                    }

                    @Override
                    public void cancel() {

                    }
                });
            }
        });
    }
    
    private void loadNetWorkData() {
        showLoding();
        BaseHttp.getInstance().query("release/releaseDetails", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
//                ConvenientLifeZhaoPinInfo convenientLifeZhaoPinInfo = new Gson().fromJson(response.toString(), ConvenientLifeZhaoPinInfo.class);
//                handleData(convenientLifeZhaoPinInfo);
                JSONObject list = response.optJSONObject("data");
                titles.setText(list.optString("title"));
                salary.setText(list.optString("xinzi"));
                small_title.setText(list.optString("areaid") + "|" + list.optString("occupation_name") + "|" + "招聘" + list.optString("renshu") + "人");
                time.setText(list.optString("posttime"));
                company.setText(list.optString("gongsi"));
                describe.setText(list.optString("content"));
                Address = list.optString("address");
                Addresslat = list.optString("lat");
                Addresslng = list.optString("lng");
                address.setText(list.optString("address"));
                contacts.setText("联系人：" + list.optString("contacts"));
                phone.setText("联系电话：" + list.optString("tel"));
                listUrl = new ArrayList<>();
                JSONArray pics = list.optJSONArray("pics");
                if (pics != null && pics.length()>0){
                    for (int i = 0;i<pics.length();i++){
                        listUrl.add(pics.optString(i));
                    }
                }
                dismissLoding();
                bindTopBannerData(listUrl);
            }
        });

    }
    
    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    /**
     * 绑定banner数据
     */
    private void bindTopBannerData(List<String> listUrl) {
        head_banner.setAdapter(new BGABanner.Adapter<View, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, String url, int position) {
                SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.sdv_item_fresco_content);
                simpleDraweeView.setImageURI(url);
            }
        });
        head_banner.setData(R.layout.homerecycle_top_banner_content, listUrl, null);
    }

    private void call(String phoneNum){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
